package com.fengwk.support.jdbc;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fengwk.support.util.MapUtils;
import com.fengwk.support.util.StringUtils;

/**
 * jdbc集群
 * 
 * @author fengwk
 * @version 创建时间：2017年7月20日 下午3:29:45
 */
public class JdbcDatabaseClusters implements JdbcDatabaseApi<JdbcDatabaseClusters> {

	private Map<String, JdbcDatabase> jdbcDatabaseMap;// JdbcDatabaseMap
	private String currentJdbcDatabaseName;
	private boolean showSql = false;
	
	public JdbcDatabaseClusters() {}
	
	/**
	 * 切换JdbcDatabase实例
	 */
	public JdbcDatabaseClusters switchJdbcDatabase(String jdbcDatabaseName) {
		this.currentJdbcDatabaseName = jdbcDatabaseName;
		return this;
	}
	
	/**
	 * 添加一个JdbcDatabase实例
	 * 
	 * @param jdbcDatabaseName
	 * @param jdbcDatabase
	 */
	public JdbcDatabaseClusters addJdbcDatabase(String jdbcDatabaseName, JdbcDatabase jdbcDatabase) {
		this.currentJdbcDatabaseName = jdbcDatabaseName;
		if(StringUtils.isNotBlank(jdbcDatabaseName)) {
    		if(this.jdbcDatabaseMap == null) {
        		this.jdbcDatabaseMap = new HashMap<String, JdbcDatabase>();
        	}
    		this.jdbcDatabaseMap.put(jdbcDatabaseName, jdbcDatabase);
    	}
    	return this;
	}
	
	/**
	 * 移除一个JdbcDatabase实例
	 * 
	 * @param jdbcDatabaseName
	 */
	public JdbcDatabaseClusters removeJdbcDatabase(String jdbcDatabaseName) {
		this.jdbcDatabaseMap.remove(jdbcDatabaseName);
		return this;
	}
	
	 /**
     * 获取JdbcDatabase实例
     * 
     * @param dataSourceName
     * @return
     */
    public JdbcDatabase getJdbcDatabase(String jdbcDatabaseName) {
    	JdbcDatabase jdbcDatabase = this.jdbcDatabaseMap.get(jdbcDatabaseName);
    	jdbcDatabase.setShowSql(this.showSql);
    	jdbcDatabase.setShowSqlPre("[" +jdbcDatabaseName + "] ==> ");
    	return jdbcDatabase;
    }

	@Override
	public JdbcDatabaseClusters openTransaction() throws JdbcException {
		currentJdbcDatabase().openTransaction();
		return this;
	}

	@Override
	public JdbcDatabaseClusters commitTransaction() throws JdbcException {
		currentJdbcDatabase().commitTransaction();
		return this;
	}

	@Override
	public JdbcDatabaseClusters rollbackTransaction() throws JdbcException {
		currentJdbcDatabase().rollbackTransaction();
		return this;
	}

	@Override
	public int update(String sql, Object... params) throws JdbcException {
		return currentJdbcDatabase().update(sql, params);
	}

	@Override
	public <B> List<B> query(String sql, Class<B> beanClass, Object... params) throws JdbcException {
		return currentJdbcDatabase().query(sql, beanClass, params);
	}

	@Deprecated
	@Override
	public <B, E> List<B> queryWithCollection(String sql, Class<B> beanClass, Collection<E> collection) throws JdbcException {
		return currentJdbcDatabase().queryWithCollection(sql, beanClass, collection);
	}

	@Override
	public <B> B querySingle(String sql, Class<B> beanClass, Object... params) throws JdbcException {
		return currentJdbcDatabase().querySingle(sql, beanClass, params);
	}

	@Deprecated
	@Override
	public <B, E> B querySingleWithCollection(String sql, Class<B> beanClass, Collection<E> collection) throws JdbcException {
		return currentJdbcDatabase().querySingleWithCollection(sql, beanClass, collection);
	}

	@Override
	public List<Map<String, Object>> query(String sql, Object... params) throws JdbcException {
		return currentJdbcDatabase().query(sql, params);
	}

	@Deprecated
	@Override
	public <E> List<Map<String, Object>> queryWithCollection(String sql, Collection<E> collection) throws JdbcException {
		return currentJdbcDatabase().queryWithCollection(sql, collection);
	}

	@Override
	public Long getLastInsertId() throws JdbcException {
		return currentJdbcDatabase().getLastInsertId();
	}
	
	public Map<String, JdbcDatabase> getJdbcDatabaseMap() {
		return jdbcDatabaseMap;
	}

	public void setJdbcDatabaseMap(Map<String, JdbcDatabase> jdbcDatabaseMap) {
		this.jdbcDatabaseMap = jdbcDatabaseMap;
	}

	public String getCurrentJdbcDatabaseName() {
		// currentJdbcDatabase不存在时设置为任意一个JdbcDatabase
		if(this.currentJdbcDatabaseName == null && MapUtils.isNotBlank(this.jdbcDatabaseMap)) {
			this.currentJdbcDatabaseName = jdbcDatabaseMap.keySet().iterator().next();
		}
		return currentJdbcDatabaseName;
	}

	public void setCurrentJdbcDatabaseName(String currentJdbcDatabaseName) {
		this.currentJdbcDatabaseName = currentJdbcDatabaseName;
	}

	public boolean isShowSql() {
		return showSql;
	}

	public void setShowSql(boolean showSql) {
		this.showSql = showSql;
	}
	
	private JdbcDatabase currentJdbcDatabase() {
		return getJdbcDatabase(getCurrentJdbcDatabaseName());
	}
	
}
